package com.ocom.payhandle.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.PayHandleUrl;
import com.ocom.common.entity.card.EntityConsTypeConfig;
import com.ocom.common.entity.card.EntityXfDeviceInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payhandle.ConsTypeConfigRequest;
import com.ocom.common.request.payhandle.GetDeviceInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pyhandle.MachConsTypeVo;
import com.ocom.payhandle.service.ConsTypeConfigService;
import com.ocom.payhandle.service.DeviceInfoService;
import com.ocom.payhandle.service.XfSignNoService;
import com.ocom.payhandle.vo.DeviceInfoVo;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class DeviceInfoController {

    @Autowired
    DeviceInfoService deviceInfoService;

    @Autowired
    ConsTypeConfigService consTypeConfigService;

    @Autowired
    XfSignNoService xfSignNoService;

    @Resource
    private RedisUtil redisUtil;

    @PostMapping(PayHandleUrl.URL_getDeviceInfo)
    public Result getDeviceInfo(@RequestBody GetDeviceInfoRequest getDeviceInfoRequest){
        Long timeLong= (long) (4 * 60 * 60);
        //查询redis是否为空，不为空把值存入redis
        String deviceInfoVoRedis="xf_device_info#comId:"+getDeviceInfoRequest.getComId()+"#machNo:"+getDeviceInfoRequest.getMachNo()+"#";
        Object objectDeviceInfo=redisUtil.get(deviceInfoVoRedis);
        DeviceInfoVo deviceInfoVo=null;
        if (objectDeviceInfo == null) {
            deviceInfoVo=deviceInfoService.selectPointId(getDeviceInfoRequest.getComId(),getDeviceInfoRequest.getMachNo());
            if (deviceInfoVo!=null){
                redisUtil.set(deviceInfoVoRedis, deviceInfoVo,timeLong);
            }
        }else {
            deviceInfoVo = JSON.parseObject(JSON.toJSONString(objectDeviceInfo), DeviceInfoVo.class);
        }

        return ResultUtil.success(deviceInfoVo);
    }


    @PostMapping(PayHandleUrl.URL_consTypeConfig)
    public Result consTypeConfig(@RequestBody ConsTypeConfigRequest request) {
        MachConsTypeVo machConsTypeVo =new MachConsTypeVo();
        DeviceInfoVo deviceInfoVo = deviceInfoService.selectPointId(request.getComId(), request.getMachNo());
        if (deviceInfoVo == null) {
            return ResultUtil.error(ResultEnum.Err_2023);
        }

        EntityXfDeviceInfo entityXfDeviceInfo = deviceInfoService.GetDeviceInfo(request.getComId(), request.getMachNo());
        if (entityXfDeviceInfo == null) {
            return ResultUtil.error(ResultEnum.Err_2023);
        }

        EntityConsTypeConfig entityConsTypeConfig = consTypeConfigService.selectEntityConsTypeConfig(request.getComId(), entityXfDeviceInfo.getConsType());


        if(entityConsTypeConfig==null){
            return ResultUtil.error(ResultEnum.Err_6005);
        }

        machConsTypeVo.setConsType(entityConsTypeConfig.getConsType());
        machConsTypeVo.setConsName(entityConsTypeConfig.getConsName());
        machConsTypeVo.setWalletSerial(entityConsTypeConfig.getWalletSerial());
        machConsTypeVo.setWithholdSerial(entityConsTypeConfig.getWithholdSerial());

        //有代扣
        if (entityConsTypeConfig.getWithholdSerial() != 0) {
            if(deviceInfoVo.getMerchId()==null){
                deviceInfoVo.setMerchId(0l);
            }
            int sign = xfSignNoService.existSignNo(request.getComId(),deviceInfoVo.getMerchId().intValue(),request.getPId());
            if(sign>0) machConsTypeVo.setSign(true);
        }
        return ResultUtil.success(machConsTypeVo);

    }
}
